/*
 * @Author: weidewei
 * @Date: 2025-05-19 09:09:42
 * @LastEditors: weidewei
 * @LastEditTime: 2025-05-21 17:49:58
 * @Description:
 * @FilePath: \v3\vite.config.ts
 */
import { defineConfig } from "vite";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import  viteCompression from "vite-plugin-compression";
import vue from "@vitejs/plugin-vue";
// import { visualizer } from 'rollup-plugin-visualizer'
import path from "path";
import MarkdownPlugin from './src/plugins/vite-plugin-md';

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver({ importStyle: 'css' })], // 关键配置
    }),
    Components({
      resolvers: [ElementPlusResolver({ importStyle: 'css' })], // 关键配置
    }),
    // viteCompression({
    //   verbose: true, // 输出压缩日志
    //   disable: false, // 是否强制启用压缩
    //   threshold: 10240,  // 对超过10kb的文件进行压缩
    //   algorithm: 'gzip',// 压缩算法
    //   ext: '.gz', // 生成的压缩包后缀
    // }),
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'brotliCompress',
      ext: '.br',
    }),
    MarkdownPlugin()
    // visualizer({
    //   filename: 'report.html',
    //   open: true,
    //   gzipSize: true,
    //   brotliSize: true
    // })
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
      "#": path.resolve(__dirname, "./types"),
      'element-plus/es': path.resolve(__dirname, 'node_modules/element-plus/lib'),
    },
  },
  // vite.config.ts
  server: {
    proxy: {
      "/api": {
        target: "http://shanhe.kim/api/za/tianqi.php",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""), // 移除多余层级
      },
    },
  },
  build: {
    rollupOptions: {
      output: {
        // manualChunks: {
        //   'element-plus': ['element-plus'],
        //   'vue-router': ['vue-router'],
        //   'axios': ['axios'],
        //   'pinia': ['pinia'],
        //   'pinia-plugin-persistedstate': ['pinia-plugin-persistedstate'],
        //   'vue': ['vue']
        // }
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return 'vendor-node_modules'
          }
        }
      }
    }
  }
});
